class ActionView::Helpers::JavaScriptElementProxy
			def replace_html(*options_for_render)
				call 'update', @generator.send(:render, *options_for_render)
			end
end
module ActionView::Helpers::PrototypeHelper
	def options_for_ajax(options)
		js_options = build_callbacks(options)

		js_options['asynchronous'] = options[:type] != :synchronous
		js_options['method']       = method_option_to_s(options[:method]) if options[:method]
		js_options['insertion']    = "Insertion.#{options[:position].to_s.camelize}" if options[:position]
		js_options['evalScripts']  = options[:script].nil? || options[:script]
		js_options['update']	   = "'#{options[:update]}'" if options[:update]
		js_options['method']	   = "'get'"

		if options[:form]
			js_options['data'] = 'this.toQueryString()'
		elsif options[:submit]
			js_options['data'] = "Object.toQueryString('#{options[:submit]}')"
		elsif options[:with]
			js_options['data'] = options[:with]
		end

		options_for_javascript(js_options)
	end

	def remote_function(options)
		javascript_options = options_for_ajax(options)

		function = 'new Ajax('

		url_options = options[:url]
		url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)
		function << "'#{url_for(url_options)}'"
		function << ", #{javascript_options}).request()"

		function = "#{options[:before]}; #{function}" if options[:before]
		function = "#{function}; #{options[:after]}" if options[:after]
		function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
		function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]

		return function
	end

	def build_callbacks(options)
		callbacks = {}
		__callbacks = CALLBACKS.to_a.concat([:request])
		options.each do |callback, code|
			if __callbacks.include?(callback)
				name = 'on' + callback.to_s.capitalize
				callbacks[name] = "function(request){#{code}}"
			end
		end
		callbacks
	end
end